package com.mingqi.enetconnected.primary.mytask.activity;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.primary.mytask.adapter.ScheduleChartAdapter;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class MyScheduleActivity extends BaseActivity implements CalendarView.OnDateSelectedListener,
        CalendarView.OnYearChangeListener, CalendarView.OnMonthChangeListener{

    @BindView(R.id.cv_show)
    CalendarView mCvShow;
    @BindView(R.id.rv_route)
    RecyclerView mRvRoute;
    @BindView(R.id.cl_schedule)
    CalendarLayout mClSchedule;

    @BindView(R.id.tv_year_month)
    TextView mTvYearMonth;

    private int mYear;
    private int mMonth;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_my_schedule;
    }

    @Override
    protected void initView() {

        setTitleName(ResourceUtils.getString(R.string.schedule_table));
        setRightTextView(ResourceUtils.getString(R.string.create_schedule), new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ToastUtils.tMessage("点击了排班");
            }
        });

        mTvYearMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!mClSchedule.isExpand()) {
                    mCvShow.showYearSelectLayout(mYear);
                    return;
                }

                if(mCvShow.isYearSelectLayoutVisible()) {

                    mCvShow.closeYearSelectLayout();
                    setDateTitle(mYear,mMonth);
                } else {

                    mCvShow.showYearSelectLayout(mYear);
                    setYearTitle(mYear);
                }
            }
        });

        mYear = mCvShow.getCurYear();
        mMonth = mCvShow.getCurMonth();
        setDateTitle(mYear,mMonth);
    }

    private void setYearTitle(int year){

        setDateTitle(year,-1);
    }
    private void setDateTitle(int year,int month){

        if(month == -1) {

            String dateFormat = ResourceUtils.getString(R.string.year);
            String dateStr = String.format(dateFormat, year);
            mTvYearMonth.setText(dateStr);

        } else {

            String dateFormat = ResourceUtils.getString(R.string.year_month);
            String dateStr = String.format(dateFormat, year,month);
            mTvYearMonth.setText(dateStr);
        }
    }

    @Override
    protected void initClick() {

        mCvShow.setOnDateSelectedListener(this);
        mCvShow.setOnYearChangeListener(this);
        mCvShow.setOnMonthChangeListener(this);
    }

    @Override
    protected void initData() {

        int redPoint = 0XFFE53232;
        int greenPoint = 0XFF2ADB31;

        Map<String, Calendar> map = new HashMap<>();
        map.put(getSchemeCalendar(mYear, mMonth, 3, redPoint, "红色").toString(),
                getSchemeCalendar(mYear, mMonth, 3, redPoint, "红色"));
        map.put(getSchemeCalendar(mYear, mMonth, 6, greenPoint, "绿色").toString(),
                getSchemeCalendar(mYear, mMonth, 6, greenPoint, "绿色"));
        map.put(getSchemeCalendar(mYear, mMonth, 9, redPoint, "红色").toString(),
                getSchemeCalendar(mYear, mMonth, 9, redPoint, "红色"));
        map.put(getSchemeCalendar(mYear, mMonth, 13, redPoint, "红色").toString(),
                getSchemeCalendar(mYear, mMonth, 13, redPoint, "红色"));
        map.put(getSchemeCalendar(mYear, mMonth, 14, greenPoint, "绿色").toString(),
                getSchemeCalendar(mYear, mMonth, 14, greenPoint, "绿色"));
        map.put(getSchemeCalendar(mYear, mMonth, 15, greenPoint, "绿色").toString(),
                getSchemeCalendar(mYear, mMonth, 15, greenPoint, "绿色"));
        map.put(getSchemeCalendar(mYear, mMonth, 18, redPoint, "红色").toString(),
                getSchemeCalendar(mYear, mMonth, 18, redPoint, "红色"));
        map.put(getSchemeCalendar(mYear, mMonth, 25, greenPoint, "绿色").toString(),
                getSchemeCalendar(mYear, mMonth, 25, greenPoint, "绿色"));
        map.put(getSchemeCalendar(mYear, mMonth, 27, redPoint, "红色").toString(),
                getSchemeCalendar(mYear, mMonth, 27, redPoint, "红色"));
        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        mCvShow.setSchemeDate(map);

        mRvRoute.setLayoutManager(new LinearLayoutManager(this));

        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {

            strings.add("日程====" + i);
        }
        ScheduleChartAdapter scheduleChartAdapter = new ScheduleChartAdapter(strings);
        mRvRoute.setAdapter(scheduleChartAdapter);

        scheduleChartAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ToastUtils.tMessage(adapter.getData().get(position));
            }
        });
    }


    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {

        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);
        calendar.setScheme(text);
        return calendar;
    }

    private boolean isFirst = true;
    @SuppressLint("SetTextI18n")
    @Override
    public void onDateSelected(Calendar calendar, boolean isClick) {

        if(isFirst) {

            isFirst = false;
            return;
        }
        mYear = calendar.getYear();
        mMonth = calendar.getMonth();
        int day = calendar.getDay();
        setDateTitle(mYear,mMonth);

        String scheme = calendar.getScheme();

        if(TextUtils.isEmpty(scheme)) {

            scheme = "没有标记";
        }

        ToastUtils.showHint("点击了" + day + "号" + "===" + scheme,mClSchedule);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onMonthChange(int year, int month) {
//        Calendar calendar = mCvShow.getSelectedCalendar();
        mYear = year;
        mMonth = month;
        setDateTitle(mYear,month);
    }

    @Override
    public void onYearChange(int year) {

        mYear = year;
        setYearTitle(mYear);
    }
}
